package controllers

import (
	"github.com/astaxie/beego"
	"travelservia/models"
)

type XiangmuController struct {
	beego.Controller
}
func (c *XiangmuController) Prepare()  {
	c.Data["headlink"]="xiangmu"
}

func (c *XiangmuController)GetList() {
	typestr := c.GetString("type")
	tacleXiangmuSearch(&c.Controller)
	xiangmus := []models.Xiangmu{}
	counts, err := models.GetXiangmuListCount()
	if err != nil {
		Message(&c.Controller, `/`, "获取列表错误即将返回首页", "", false)
	}
	numPerPage := 40
	p := SetPaginator(&c.Controller, numPerPage, counts)
	CurrentPage := p.Page()

	if typestr == "hot" {
		models.GetXiangmuHotList(&xiangmus, (CurrentPage - 1) * numPerPage, numPerPage)
	} else if typestr == "newest" {
		models.GetXiangmuList(&xiangmus, (CurrentPage - 1) * numPerPage, numPerPage)
	} else {
		models.GetXiangmuList(&xiangmus, (CurrentPage - 1) * numPerPage, numPerPage)
	}
	c.Data["xiangmu"] = &xiangmus
	c.Data["listpage"]=true
	c.TplName = "xiangmulist.html"
}
func tacleXiangmuSearch(c *beego.Controller) {
	search:=c.GetString("search")
	if search==""{
		return
	}
	xiangmus := []models.Xiangmu{}
	counts, err := models.GetXiangmuSearchCount(search)
	if err != nil {
		Message(c, `/`, "获取列表错误即将返回首页", "", false)
	}
	numPerPage := 40
	p := SetPaginator(c, numPerPage, counts)
	CurrentPage := p.Page()
	models.GetXiangmuHotList(&xiangmus, (CurrentPage - 1) * numPerPage, numPerPage)
	c.Data["xiangmu"]=&xiangmus
	c.TplName="xiangmulist.html"
	c.Render()
	c.StopRun()
}


func (c *XiangmuController)GetDetail() {
	id, _ := c.GetInt("id")
	xiangmu := models.Xiangmu{Id:id}
	if err := models.GetXiangmuDetail(&xiangmu); err != nil {
		Message(&c.Controller, `/`, "获取列表错误即将返回首页", "", false)
	}
	c.Data["xiangmu"] = &xiangmu
	c.TplName = "xiangmudetail.html"
}

func (c *XiangmuController)AddBrowseCount() {
	id, _ := c.GetInt("id")
	if id == 0 {
		Json_Message(&c.Controller, 1, "id not right")
	}
	xiangmu := models.Xiangmu{Id:id}
	if err := models.AddXiangmuBrowseCount(&xiangmu); err != nil {
		Json_Message(&c.Controller, 1, "添加计数失败")
	}
	Json_Message(&c.Controller, 0, "success！")
}
func (c *XiangmuController)AddStartCount() {
	id, _ := c.GetInt("id")
	if id == 0 {
		Json_Message(&c.Controller, 1, "id not right")
	}
	xiangmu := models.Xiangmu{Id:id}
	if err := models.AddXiangmuStarCount(&xiangmu); err != nil {
		Json_Message(&c.Controller, 1, "添加点赞失败")
	}
	Json_Message(&c.Controller, 0, "success！")
}